612D - The Union of k-Segments - CodeForces Solution


greedy sortings *1800

Please click on ads to support us..

Python Code:

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n, k = map(int, input().split())
event = []
for i in range(n):
    l, r = map(int, input().split())
    event.append(2*l)
    event.append(2*r+1)

event.sort()
cur = 0
ans = []
for x in event:
    if x%2 == 0:
        if cur+1 == k and cur < k:
            start = x//2
        cur += 1
    else:
        if cur == k and cur-1 < k:
            ans.append((start, x//2))
        cur -= 1
print(len(ans))
for l, r in ans:
    print(l, r)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

using ll = long long;

template<typename T> void min_self( T &a, const T b ) { if(b < a) a = b; }
template<typename T> void max_self( T &a, const T b ) { if(b > a) a = b; }

const int inf = (int)1e9 + 7;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, k;
	cin >> n >> k;
	
	vector<array<int, 2>> p(n);
	vector<pair<int, int>> events;
	
	for(auto &[x, y] : p){
		cin >> x >> y;
		events.emplace_back(x, 1);
		events.emplace_back(y, -1);
	}
	
	#define pii pair<int, int>
	sort(events.begin(), events.end(), [&](pii a, pii b){
		if(a.first == b.first)
			return a.second > b.second;
		return a.first < b.first;
	});

	vector<pair<int, int>> segs;
	
	int m = (int)events.size();
	int c = 0;
	int who = inf;
	
	for(int i = 0;i < m;i++){
		c += events[i].second;
		if(c == k - 1 && events[i].second == -1){
			assert(who != inf);
			segs.emplace_back(who, events[i].first);
			who = inf;
		}else if(c >= k){
			if(who == inf) who = events[i].first;
		}
	}
	
	cout << (int)segs.size() << "\n";
	for(auto [X, Y] : segs){
		cout << X << " " << Y << "\n";
	}

	return 0x0;
}


Comments

Submit
0 Comments
More Questions

961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers